package com.appcheck.recording.listener.impl;

import java.lang.reflect.Field;

import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.appcheck.recording.ListenerManager;
import com.appcheck.recording.action.impl.KeyAction;
import com.appcheck.recording.listener.AbstractListener;
import com.appcheck.recording.utils.Logger;

public class EditorActionListener extends AbstractListener implements TextView.OnEditorActionListener{

	private int b;
	
	public EditorActionListener(TextView paramTextView) {
		super( paramTextView, TextView.class, TextView.OnEditorActionListener.class);
	}

	@Override
	public final boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		if(this.b++ > 1){
			return false;
		}
	    if (((event == null) || (event.getAction() != android.view.KeyEvent.ACTION_DOWN)) && 
	    		((actionId == android.view.inputmethod.EditorInfo.IME_ACTION_DONE) || 
	    				(actionId == android.view.inputmethod.EditorInfo.IME_ACTION_NEXT) || 
	    				(actionId == android.view.inputmethod.EditorInfo.IME_ACTION_GO) || 
	    				(actionId == android.view.inputmethod.EditorInfo.IME_ACTION_SEARCH) || 
	    				(actionId == android.view.inputmethod.EditorInfo.IME_ACTION_UNSPECIFIED))){
	    	 KeyAction keyAction = new KeyAction();
	    	 keyAction.setKeyCode(android.view.KeyEvent.KEYCODE_ENTER);
	    	 Logger.DEBUG("create keyAction " + keyAction.toString());
	    	 ListenerManager.getListenerManager().handleAction(keyAction);
	    }
	    if (this.listener != null){
	    	boolean bool = ((TextView.OnEditorActionListener)this.listener).onEditorAction(v, actionId, event);      
	    	this.b = 0;      
	    	return bool;
	    }
	    this.b = 0;
	    	    
	    return false;
	}
	/**
	 * 获取应用本来的listener</br>
	 * 注意TextView里面的onEditorActionListener是在mEditor.inputContentType中
	 * {@link TextView#setOnEditorActionListener}
	 */
	@Override
	protected final Object getListenerFromView(View paramView, Class viewClass, Class listenerClass) throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException{
		Field editorField = null;
		//根据sdk版本获得mEditor
	    if (Build.VERSION.SDK_INT >= 16) {
		    try{
		        (editorField = viewClass.getDeclaredField("mEditor")).setAccessible(true);
		    }
		    catch (SecurityException localSecurityException) {
		    	Logger.ERROR("SecurityException", localSecurityException);
		    }
		    catch (NoSuchFieldException localNoSuchFieldException){
		    	Logger.ERROR("NoSuchFieldException", localNoSuchFieldException);
		    }
	    }
	    //如果有mEditor则获取该字段的值
	    Object editor = null;
	    if (editorField != null) {
	    	editor = editorField.get(paramView);
	    }
	    //如果editor不为空则从editor获取InputContentType，否则从paramClass1中直接获取
	    Field inputContentTypeField = null;
	    (inputContentTypeField = (editor != null ? editor.getClass().getDeclaredField("mInputContentType") : viewClass.getDeclaredField("mInputContentType"))).setAccessible(true);
	    Object inputContentType = null;
	    if ((inputContentType = inputContentTypeField.get(editor != null ? editor : paramView)) != null){
	    	//TextView里面的onEditorActionListener是在mEditor.inputContentType中,如果版本低直接在inputContentType中
	    	Field onEditorActionListenerField = inputContentType.getClass().getDeclaredField("onEditorActionListener");
	    	onEditorActionListenerField.setAccessible(true);
	    	return onEditorActionListenerField.get(inputContentType);
	    }
	    return null;
	  }

}
